Ext.define('YX.core.ClientUpgrade',{
    extend:'Ext.panel.Panel',
    layout:{
        type:'vbox',
        align : 'stretch'
    },
    frmUpgrade:null,
    frmFile:null,
    btnSave:null,
    cmbAppType:null,
    cmbClientType:null,
    txtClientVer:null,
    fileUpgrade:null,
    dspFile:null,
    fileId:null,
    grdUpgrade:null,
    initComponent:function(){
        this.btnSave = Ext.create('Ext.button.Button',{
            text:T.label.save,
            itemId : 'btnSave',
            iconCls : 'icon-disk',
            scope : this,
            handler : this.onBtnSaveClicked
        });

        this.cmbAppType = Ext.create('Ext.form.field.ComboBox',{
            fieldLabel:T.label.appType,
            labelWidth:80,
            name:'appType',
            displayField:'text',
            valueField:'value',
            editable:false,
            value:'MES',
            store:Ext.create('Ext.data.ArrayStore',{
                fields:['value','text'],
                autoLoad:true,
                autoDestroy:true,
                data:[['MES','MES'],['WMS','WMS']]
            })
        });

        this.cmbClientType = Ext.create('Ext.form.field.ComboBox',{
            fieldLabel:T.label.clientType,
            labelWidth:80,
            name:'clientType',
            displayField:'text',
            valueField:'value',
            editable:false,
            value:'Android',
            store:Ext.create('Ext.data.ArrayStore',{
                fields:['value','text'],
                autoLoad:true,
                autoDestroy:true,
                data:[['Android','Android'],['Web','Web']]
            })
        });

        this.txtClientVer = Ext.create('Ext.form.field.Text',{
            fieldLabel:T.label.version,
            allowBlank:false,
            minLength:1,
            maxLength:10,
            labelWidth:80,
            name:'clientVerName'
        });

        this.fileUpgrade = Ext.create('Ext.form.field.File',{
            fieldLabel:T.label.file,
            labelWidth:80,
            flex:1,
            listeners:{
                scope:this,
                change:this.onFileChange
            }
        });

        this.dspFile = Ext.create('Ext.form.field.Display',{flex:1});

        this.frmUpgrade = Ext.create('Ext.form.Panel',{
            title:T.label.definition,
            bodyBorder :false,
            border: false,
            bodyPadding : "5px 0 0 0",
            tbar:[this.btnSave],
            items:[{
                xtype:'fieldcontainer',
                layout:'hbox',
                defaults:{
                    flex:1
                },
                items:[this.cmbAppType,this.cmbClientType,this.txtClientVer]
            }
            ]
        });

        this.frmFile= Ext.create('Ext.form.Panel',{
            bodyBorder :false,
            border: false,
            items:[{
                xtype:'fieldcontainer',
                layout:'hbox',
                items:[this.fileUpgrade,this.dspFile,{
                    "xtype":"displayfield",
                    "flex":2
                }]
            }]
        });

        this.grdUpgrade = Ext.create('Ext.grid.Panel',{
            title:T.label.detail,
            flex:1,
            bbar:{
                xtype:'pagingtoolbar',
                displayInfo:true
            },
            columns:[{
                text:T.label.appType,
                dataIndex:'appType',
                width:120
            },{
                text:T.label.clientType,
                dataIndex:'clientType',
                width:120
            },{
                text:T.label.version,
                dataIndex:'clientVerName',
                width:100
            },{
                text:T.label.file,
                dataIndex:'fileName',
                width:200
            },{
                text:T.label.updateDate,
                width:150,
                dataIndex:'updateDate',
                xtype: 'datecolumn',
                format:'Y-m-d H:i:s'
            }],
            store:{
                xtype:'json',
                autoLoad:true,
                fields:['appType','id','clientType','clientVerName','fileId','fileName',{
                    name:'updateDate',
                    type:'date',
                    dateFormat:'YmdHis'
                }],
                proxy:{
                    type:'zbusrpc',
                    module:'BasViewUpgradeClientService',
                    extraParams:{
                        pageSize:80
                    },
                    reader:{
                        root:'upgradeList'
                    }
                }
            },
            listeners:{
                scope:this,
                select:this.onGrdUpgradeSelect
            }
        });

        this.items=[this.frmFile,this.frmUpgrade,this.grdUpgrade];

        this.callParent();
    },
    onAfterRender:function(){
        this.grdUpgrade.getDockedItems('toolbar[dock="bottom"]').bindStore(this.grdUpgrade.getStore());
    },
    onBtnSaveClicked:function(btn){
        if(!this.txtClientVer.isValid()){
            return;
        }

        Ext.Msg.show({
            title : T.label.save,
             msg : T.msg.are_you_sure_to_save_data,
             icon : Ext.Msg.QUESTION,
             buttons : Ext.Msg.YESNO,
             scope : this,
             fn : function(btnId) {
             if (btnId == 'yes') {
                btn.disable();
                this.saveVer(btn);
               }
              }
    		});
    },saveVer:function(btn){
        var data = this.frmUpgrade.getValues();
        data.fileId = this.fileId;

        var me = this;
        YX.ZbusRpc.request({
            module : 'BasSaveUpgradeClientService',
    		scope : this,
    		showSuccess : true,
    		params : {
    		    update : data
    		},
    		callback : function() {
    			btn.enable();
    			me.frmUpgrade.getForm().reset();
    			me.frmFile.getForm().reset();
    		},
    		success : function() {
    			this.grdUpgrade.getStore().reload();
    		}
    	});
    },onFileChange:function(field,value){
        if(value===''){
            return;
        }

        var me = this;

		this.frmFile.submit({
		    url: 'file/upload',
		    clientValidation: false,
		    params: {
                    mode: 'S',
                    group:'upgrade'
            },
		    success: function(form, action) {
		        if (action.result.rspCode === 'SYS-0000') {
		            me.fileId = action.result.fileList[0].fileId;
		            me.dspFile.setValue(value+" " +T.msg.file_staged);
		        } else {
		            me.dspFile.setValue(action.result.rspDesc);
		        }
		    },
		    failure: function(form, action) {
		        if (action.result.rspCode === 'SYS-0000') {
		            me.fileId = action.result.fileList[0].fileId;
		            me.dspFile.setValue(value+" " +T.msg.file_staged);
                } else {
                    me.dspFile.setValue(action.result.rspDesc);
                }
		    }
		});
    },onGrdUpgradeSelect:function(grid,record){
        this.frmUpgrade.loadRecord(record);
        this.fileId=record.get('fileId');
    }
});